﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Isha.Entities;
using Isha.Infrastructure;
using Isha.Web.Mvc;

namespace Isha.Extensions.Link
{
    [Command(typeof(Link))]
    public class LinkCommand : IContentCommand
    {
        public void Execute()
        {
            if (!IshaContext.Current.Request.IsAuthenticated)
                IshaContext.Current.Response.HttpResponse.RedirectPermanent(IshaContext.Current.Model.Get<Link>().ExternalLink);

            if (IshaContext.Current.Request.FormCollection != null && (IshaContext.Current.Command == Commands.Create || IshaContext.Current.Command == Commands.Edit))
            {
                ContentItem contentItem = IshaContext.Current.ContentItem;

                contentItem.ContentXml = IshaContext.Current.Serializer(contentItem.Type, new Link()
                {
                    ExternalLink = IshaContext.Current.Request.FormCollection["Link_ExternalLink"]
                });

                IshaContext.Current.ContentItem = contentItem;
            }
        }
    }
}
